package com.bestcem.xm.ticket.mq.receiver.biz;

import cn.hutool.json.JSONUtil;
import com.bestcem.xm.ticket.dao.TicketDao;
import com.bestcem.xm.ticket.entity.mongo.Ticket;
import com.bestcem.xm.ticket.enums.TicketEventEnum;
import com.bestcem.xm.ticket.service.TicketMessageReceiverService;
import com.bestcem.xm.ticket.service.TicketNotificationService;
import com.bestcem.xm.ticket.service.dto.OrgCreatedMessage;
import com.bestcem.xm.ticket.service.dto.mq.*;
import com.bestcem.xm.ticket.service.manager.fsm.TicketFsmManager;
import com.bestcem.xm.ticket.service.manager.record.param.TransferParam;
import com.bestcem.xm.ticket.task.NotificationTask;
import lombok.extern.slf4j.Slf4j;
import org.springframework.messaging.support.GenericMessage;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@Slf4j
@Component
public class ReceiverService {
    //@Resource
    //private TicketMqConsumUtil ticketMqConsumUtil;

    @Resource
    private TicketMessageReceiverService ticketMessageReceiverService;

    @Resource
    private TicketNotificationService ticketNotificationService;

    @Resource
    private TicketFsmManager ticketFsmManager;

    @Resource
    private TicketDao ticketDao;

    @Resource
    private NotificationTask notificationTask;

    /**
     * 工单二次投放队列消息
     */
    public void ticketDeliverAgain(GenericMessage<DeliverAgainDTO> msg) {
        DeliverAgainDTO dto = msg.getPayload();
        if (log.isInfoEnabled()) {
            log.info("[工单] 工单二次投放队列收到消息:{}", JSONUtil.toJsonStr(dto));
        }
        ticketMessageReceiverService.deliverAgainBegin(dto);
    }

    /**
     * 监听 工单预警触发队列MQ
     */
    public void ticketRuleTriggered(GenericMessage<TicketStatusUpdateDto> msg) {
        TicketStatusUpdateDto dto = msg.getPayload();
        log.info("[ticket] ticketRuleTriggered 收到消息:{}", JSONUtil.toJsonStr(dto));
        ticketMessageReceiverService.ticketStatusUpdate(dto);
    }

    /**
     * 问卷创建队列消息
     */
    public void projectCreated(GenericMessage<NotificationCreateDto> msg) {
        NotificationCreateDto dto = msg.getPayload();
        log.info("[ticket] projectCreated 收到消息:{}", JSONUtil.toJsonStr(dto));
        ticketMessageReceiverService.createNotificationByProject(dto);
    }

    /**
     * 奖励通知-工单通知触发队列消息
     */
    public void notificationTrigger(GenericMessage<NotificationTriggerDTO> msg) {
        NotificationTriggerDTO dto = msg.getPayload();
        log.info("[ticket] notificationTrigger 收到消息:{}", JSONUtil.toJsonStr(dto));
        ticketMessageReceiverService.notificationTrigger(dto);
    }

    /**
     * 用户邮箱删除队列消息
     */
    public void smtpDeleted(GenericMessage<List<String>> msg) {
        List<String> list = msg.getPayload();
        if (log.isInfoEnabled()) {
            log.info("【工单】 用户邮箱删除队列消息:{}", JSONUtil.toJsonStr(list));
        }
        ticketMessageReceiverService.clearSmtp(list);
    }

    /**
     * 租户组织创建队列消息
     */
    public void userOrgCreated(GenericMessage<OrgCreatedMessage> msg) {
        OrgCreatedMessage dto = msg.getPayload();
        if (log.isInfoEnabled()) {
            log.info("【工单】 租户组织创建队列消息:{}", JSONUtil.toJsonStr(dto));
        }
        ticketMessageReceiverService.createTicketDefaultTemplate(dto);
    }

    /**
     * 移除微信用户订阅的所有问卷
     */
    public void wechatUnsubscribe(GenericMessage<String> msg) {
        String openId = msg.getPayload();
        if (log.isInfoEnabled()) {
            log.info("【工单】 移除微信用户订阅的所有问卷:{}", JSONUtil.toJsonStr(openId));
        }
        ticketNotificationService.removeWechatUserSubscription(openId);
    }

    /**
     * 计算周期行任务通知 提醒消息 延时MQ
     */
    public void ticketSendNotify(GenericMessage<TicketSendDTO> msg) {
        TicketSendDTO dto = msg.getPayload();
        if (log.isInfoEnabled()) {
            log.info("[工单] 工单延迟队列周期任务收到消息消息体 {}", dto);
        }
        notificationTask.receiveNotifyMq(dto);
    }

    /**
     * 工单流转 发送提醒消息 延时MQ
     */
    public void ticketOverDue(GenericMessage<FollowOverdueDTO> msg) {
        FollowOverdueDTO dto = msg.getPayload();
        if (log.isInfoEnabled()) {
            log.info("[工单] 工单延迟队列逾期收到消息消息体 {}", dto);
        }
        Ticket ticket = ticketDao.findById(dto.getTicketId());
        if (Objects.isNull(ticket)) {
            if (log.isInfoEnabled()) {
                log.info("[工单] 工单逾期消息消费失败，工单查询为空; TicketId:{}", dto.getTicketId());
            }
            return;
        }
        ticketFsmManager.transfer(ticket, TicketEventEnum.getByIndex(dto.getEvent()), new TransferParam());
    }

    /**
     * 答卷提交
     */
    public void surveyRespondentSubmit(GenericMessage<TicketConfigCheckDTO> msg) {
        TicketConfigCheckDTO dto = msg.getPayload();
        log.info("[ticket] surveyRespondentSubmit收到消息{}", JSONUtil.toJsonStr(dto));
        ticketMessageReceiverService.checkTicketConfig(dto);
        ticketMessageReceiverService.checkTicketConfigStatus(dto);

    }
}
